import sys
import time
sys.path.append("D:/travodds/travodds_py/install/lib")
sys.path.append("D:/travodds/build/travoddsgen/test/generate_py/output/install/lib")
from travodds import *
from ShapeType import *

def subscriber():
    # Create DomainParticipantFactory
    factory = DomainParticipantFactoryProxy.get_instance()
    if not factory:
        print("Failed to get DomainParticipantFactory instance", file=sys.stderr)
        return
    
    dp_qos = DomainParticipantQosProxy()
    ret = factory.get_default_participant_qos(dp_qos)
    if ret != RETCODE_OK:
        print("Failed to get default DomainParticipantQos", file=sys.stderr)
        return

    # Create DomainParticipant
    participant = factory.create_participant(
        0, 
        # DomainParticipantFactoryProxy.PARTICIPANT_QOS_DEFAULT(), 
        dp_qos,
        None, 
        StatusMaskProxy.NONE
    )
    if not participant:
        print("Failed to create DomainParticipant", file=sys.stderr)
        return

    input("Press Enter to continue create subscriber...")

    # Create Subscriber
    subscriber = participant.create_subscriber(
        DomainParticipantProxy.subscriber_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )
    if not subscriber:
        print("Failed to create Subscriber", file=sys.stderr)
        return

    input("Press Enter to continue register type...")

    # Register Type
    type_support = ShapeTypeTypeSupport.get_instance()
    if not type_support:
        print("Failed to get ShapeTypeTypeSupport instance", file=sys.stderr)
        return

    ret = participant.registe_type(
        'ShapeType',
        type_support
    )
    if ret != RETCODE_OK:
        print("Failed to register ShapeType type", file=sys.stderr)
        return
    
    input("Press Enter to continue create topic...")

    # Create Topic
    topic = participant.create_topic(
        'ExampleTopic', 
        'ShapeType',
        DomainParticipantProxy.topic_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )
    if not topic:
        print("Failed to create Topic", file=sys.stderr)
        return
    
    # Cast topic to TopicDescription
    topic_desc = topic.as_topic_description()
    if not topic_desc:
        print("Failed to cast Topic to TopicDescription", file=sys.stderr)
        return

    input("Press Enter to continue create datareader...")

    # Create DataReader
    data_reader = subscriber.create_datareader(
        topic_desc, 
        SubscriberProxy.datareader_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )
    if not data_reader:
        print("Failed to create DataReader", file=sys.stderr)
        return

    input("Press Enter to continue create typed datareader...")
    
    # Create TypedDataReader
    typed_data_reader = ShapeTypeDataReader(data_reader)
    if not typed_data_reader:
        print("Failed to create TypedDataReader", file=sys.stderr)
        return

    input("Press Enter to continue ...")
    
    # loop to read data
    while True:
        # Sleep for 1 second
        time.sleep(1)
        continue

        # Create a ShapeType instance to hold the data
        shape = ShapeType()
        sample_info = SampleInfoProxy()
        
        # Read the next sample
        ret = typed_data_reader.take_next_sample(shape, sample_info)

        if ret == RETCODE_NO_DATA:
            print("No data available", file=sys.stderr)
            continue

        if ret != RETCODE_OK:
            print("Failed to read data", file=sys.stderr)
            continue
        
        # Check if sample is valid
        if sample_info.get_valid_data():
            print(f"Read ShapeType: x={shape.x}, y={shape.y}, shapesize={shape.shapesize}, color={shape.color}")
        else:
            print("Sample is not valid", file=sys.stderr)
        

if __name__ == "__main__":
    subscriber()
